/*
 * SDLWindow.cpp
 *
 *  Created on: 24/05/2014
 *      Author: javier
 */

#include "SDLWindow.h"
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL_mixer.h>
#include "SDLUtils.h"

SDLWindow::SDLWindow() {
	this->window = NULL;
	this->renderer = NULL;
	font = NULL;
}

bool SDLWindow::init() {
	//We'll just be using square tiles for now
	if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
		SDLUtils::logSDLError(std::cout, "SDL_Init");
		return false;
	}
	// Inicializar SDL_image
	if ((IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) != IMG_INIT_PNG) {
		SDLUtils::logSDLError(std::cout, "IMG_Init");
		return false;
	}

	if (Mix_OpenAudio(44100, AUDIO_U16LSB, 2, 4096) < 0) {
		printf("SDL_mixer could not initialize! SDL_mixer Error: %s\n",
		Mix_GetError());
	}
	if (TTF_Init() != 0) {
		SDLUtils::logSDLError(std::cout, "TTF_Init");
		return 1;
	}
	//Open the font
	font = TTF_OpenFont(properties.getProperty("font").c_str(),
			properties.getPropertyAsInt("font.size"));
	if (font == NULL) {
		SDLUtils::logSDLError(std::cout, "TTF_OpenFont");
		return NULL;
	}
	return true;
}

bool SDLWindow::createWindow() {
	window = SDL_CreateWindow(properties.getProperty("screen.title").c_str(), 100, 100,
			properties.getPropertyAsInt("screen.width"),
			properties.getPropertyAsInt("screen.height"), SDL_WINDOW_SHOWN);
	if (window == NULL) {
		SDLUtils::logSDLError(std::cout, "CreateWindow");
		return false;
	}
	return true;
}

bool SDLWindow::createRenderer() {
	renderer = SDL_CreateRenderer(window, -1,
			SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
	if (renderer == NULL) {
		SDLUtils::logSDLError(std::cout, "CreateRenderer");
		return false;
	}
	return true;
}

SDL_Texture* SDLWindow::loadTexture(std::string& filename) {
	return SDLUtils::loadTexture(filename, renderer);
}

SDLWindow::~SDLWindow() {
	Mix_CloseAudio();
	TTF_CloseFont(font);
	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);
	IMG_Quit();
	SDL_Quit();
}

void SDLWindow::renderTexture(SDL_Texture* tex, SDL_Rect *geometry,
		SDL_Rect* clip) {
	return SDLUtils::renderTexture(tex, renderer, geometry, clip);
}

void SDLWindow::renderTexture(SDL_Texture* tex, int x, int y,
		SDL_Rect *geometry) {
	return SDLUtils::renderTexture(tex, renderer, x, y, geometry);
}

void SDLWindow::clear() {
	SDL_RenderClear(renderer);
}

void SDLWindow::renderPresent() {
	SDL_RenderPresent(renderer);
}

/**
 * Render the message we want to display to a texture for drawing
 * @param message The message we want to display
 * @param fontFile The font we want to use to render the text
 * @param color The color we want the text to be
 * @param fontSize The size we want the font to be
 * @param renderer The renderer to load the texture in
 * @return An SDL_Texture containing the rendered message, or nullptr if something went wrong
 */
SDL_Texture* SDLWindow::renderText(const std::string &message, SDL_Color color) {
	//We need to first render to a surface as that's what TTF_RenderText
	//returns, then load that surface into a texture
	SDL_Surface *surf = TTF_RenderText_Blended(font, message.c_str(), color);
	if (surf == NULL) {
		TTF_CloseFont(font);
		SDLUtils::logSDLError(std::cout, "TTF_RenderText");
		return NULL;
	}
	SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surf);
	if (texture == NULL) {
		SDLUtils::logSDLError(std::cout, "CreateTexture");
	}
	//Clean up the surface and font
	SDL_FreeSurface(surf);
	return texture;
}
